
package com.qst.itoffer.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.qst.itoffer.bean.ApplicantBean;
import com.qst.itoffer.dao.ApplicantDao;

/**
 * 求职者登录业务处理
 */
@WebServlet("/ApplicantLoginServlet")
public class ApplicantLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ApplicantLoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//设置请求和响应编码
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out2 = response.getWriter();
		HttpSession session=request.getSession();
		//获取请求参数
		String login_name=request.getParameter("login_name");
		String login_pawd=request.getParameter("login_pawd");
		String rememberMe=request.getParameter("rememberMe");
		
		//验证登录
		ApplicantDao appDao=new ApplicantDao();
		ApplicantBean applicantBean = new ApplicantBean();
		applicantBean = appDao.valiLogin(login_name);
		
		//结构判断处理
		if(applicantBean.getApplicant_id()>0){
			//判断密码
			if(login_pawd.equals(applicantBean.getApplicant_pawd())) {
				//将登陆成功的信息存入session中
				session.setAttribute("LoginApplicant", applicantBean);
				//通过cookie记住登陆账号和密码
				rememberMe(rememberMe,login_name,login_pawd,request,response);
				//判断求职者是否填写简历
				int resumeId = appDao.isExistResume(applicantBean.getApplicant_id());
				if(resumeId>0) {
					//将简历ID存入session中
					session.setAttribute("ResumeId", resumeId);
					//已完善简历信息，重定向到首页
					response.sendRedirect("index.jsp");
				}else {
					//没有简历信息，重定向到简历填写向导页
					response.sendRedirect("applicant/resumeGuide.jsp");
				}
				
			}else {
				//登陆密码错误
				out2.println("<script type = 'text/javascript>'");
				out2.println("alert('登陆密码不正确！')");
				out2.println("window.location='login.jsp';");
				out2.println("</script>");
				
			}
		}else {
			//登录邮箱不存在
			out2.println("<script type = 'text/javascript>'");
			out2.println("alert('登陆邮箱不存在！')");
			out2.println("window.location='login.jsp';");
			out2.println("</script>");
		}
		
	}
/**
 * 记住登陆账号和密码方法cookie
 * @param rememberMe
 * @param login_name
 * @param login_pawd
 * @param request
 * @param response
 */
	private void rememberMe(String rememberMe, String login_name, String login_pawd, HttpServletRequest request,
			HttpServletResponse response) {
		if("true".equals(rememberMe)) {
			//记住登陆账号和密码
			Cookie cookie_In=new Cookie("Cookie_LoginName",login_name);
			//设置cookie的存活时间
			cookie_In.setMaxAge(365*24*60*60);
			//通过响应头的方式响应到客户端
			response.addCookie(cookie_In);
			//记住登陆账号和密码
			Cookie cookie_Ip=new Cookie("Cookie_LoginPawd",login_pawd);
			//设置cookie的存活时间
			cookie_Ip.setMaxAge(365*24*60*60);
			//通过响应头的方式响应到客户端
			response.addCookie(cookie_Ip);
		}else {
			//清空cookie中记住的登陆账号和密码
			//先获取到客户端中所有的cookie对象
			Cookie[] cookies=request.getCookies();
			if(cookies!=null) {
				for(Cookie cookie :cookies) {
					if("Cookie_LoginName".equals(cookie.getName())||"Cookie_LoginPawd".equals(cookie.getName())) {
						cookie.setMaxAge(0);
						response.addCookie(cookie);
					}
				}
			}
		}
		
	}

}
